﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace Ch09Ex03
{
    [Serializable]
    class Student : ICloneable
    {
        public string IDCode { get; set; }

        public int Age { get; set; }

        public Grent Grent { get; set; }

        #region 拷贝主体
        /// <summary>
        /// 深度拷贝(使用序列化实现深拷贝)
        /// </summary>
        /// <returns></returns>
        public Student DeepClone()
        {
            using (Stream objectStream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(objectStream, this);
                objectStream.Seek(0, SeekOrigin.Begin);
                return formatter.Deserialize(objectStream) as Student;
            }
        }
        /// <summary>
        /// 浅拷贝
        /// </summary>
        /// <returns></returns>
        public object Clone()
        {
            return this.MemberwiseClone();
        }
        #endregion
    }
    [Serializable]
    class Grent
    {
        public string Name { get; set; }

        public override string ToString()
        {
            return this.Name;
        }
    }
}
